/**
 * created by 小卷毛, 2020-05-11 011
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.aifeng.viewmodel

import android.content.Context
import com.aifeng.common_ui.helper.getJsonFromAssets
import com.aifeng.common_ui.helper.toNotDouble
import com.aifeng.common_ui.helper.toNotInt
import com.aifeng.model.AreaData
import com.aifeng.model.BaseUrl
import com.aifeng.model.CommonData
import com.aifeng.model.CommonModel
import com.aifeng.utils.applyFlowableSchedulers
import com.aifeng.utils.applySchedulers
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.lzg.extend.BaseResponse
import com.lzg.extend.jackson.JacksonConvert
import com.lzg.okrx.RxSingleHelper
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import io.reactivex.Flowable
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * 项目名称：Hassan_Food
 * 创建人：小卷毛
 * 创建时间：2020-05-11 09:44
 */
class AdressViewModel : BaseViewModel() {

    fun getAreaList(context: Context): Flowable<java.util.ArrayList<AreaData>> =
        Flowable.just("province_data.json")
            .subscribeOn(Schedulers.io())
            .map {
                val json = context.getJsonFromAssets("province_data.json")
                if (json.isEmpty()) return@map ArrayList<AreaData>()
                return@map Gson().fromJson(
                    json,
                    object : TypeToken<List<AreaData>>() {}.type
                ) as ArrayList<AreaData>
            }
            .observeOn(AndroidSchedulers.mainThread())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    fun getList(page: Int, id: String) =
        OkGo.post<BaseResponse<CommonModel>>(BaseUrl.addressList)
            .params(
                getParams(
                    "page" to page,
                    "userid" to id
                )
            )
            .converter(object : JacksonConvert<BaseResponse<CommonModel>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.results }

    fun addArea(vararg params: Pair<String, Any>) =
        RxSingleHelper.requestString(
            url = BaseUrl.addAddress,
            params = getParams(*params)
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    fun editArea(vararg params: Pair<String, Any>) =
        RxSingleHelper.requestString(
            url = BaseUrl.updateAddress,
            params = getParams(*params)
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    fun delArea(id: String) =
        RxSingleHelper.requestString(
            url = BaseUrl.delAddress,
            params = getParams("id" to id)
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    fun expressFee(
        userId: String,
        adressId: String,
        json: String
    ): Single<String> =
        RxSingleHelper.requestString(
            url = BaseUrl.expressFee,
            params = getParams(
                "userid" to userId,
                "addressid" to adressId,
                "goodlist" to json
            )
        )
            .compose(applySchedulers())

    fun calculateTotal(items: ArrayList<Any>, onResult: (String) -> Unit) {
        mDisposables.add(
            Flowable.just(items)
                .map { data ->
                    var priceTotal = 0.0
                    data.forEach {
                        it as CommonData
                        priceTotal += it.price.toNotDouble() * it.num.toNotInt()
                    }

                    return@map priceTotal.toString()
                }
                .compose(applyFlowableSchedulers())
                .subscribe { onResult(it) }
        )
    }

    fun submitOrder(vararg params: Pair<String, Any>) =
        RxSingleHelper.requestString(
            url = BaseUrl.createOrder,
            params = getParams(*params)
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

}